

<%--
  Created by IntelliJ IDEA.
  User: 86159
  Date: 2022/11/9
  Time: 9:33
  To change this template use File | Settings | File Templates.
--%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page contentType="text/html; charset=UTF-8" language="java" import="java.util.*" pageEncoding="UTF-8" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">

    <title>学生选课</title>

    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <!--<link rel="stylesheet" type="text/css" href="pages/css/studentIndex.css">  -->

    <script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
    <script type="text/javascript">
        //选择课程
        $(document).ready(
            function() {
                $("input[name='select']").click(function(){
                    var node = $(this);
                    //获取剩余量
                    var nodeId = $(this).attr("id").split("+")[0];
                    //通过td里面的元素获得tr,使用tr的children（“td”） 方法获取所有的td，在通过eq(1) 拿第二个td
                    var remainNum = $(this).parent().parent().children("td").eq(1).html();
                    console.log(remainNum);
                    if (!(remainNum == "0")) {
                        $.ajax({
                            url:"RemainAddServlet?random="+ Math.random()%10987,   //请求的url地址
                            dataType:'json',    //返回格式为json
                            async:true,    //请求是否异步，默认为异步，这也是ajax重要特性
                            data:{"id":$(this).attr("id")},    //参数值
                            type:'POST',    //请求方式
                            beforeSend:function(){
                                //请求前的处理
                               // alert("haha")
                            },
                            success:function(req){
                                //请求成功时处理
                                //node.parent().parent().remove();
                                node.parent().parent().children("td").eq(1).html(parseInt(remainNum)-parseInt(1));
                                alert("已选中！");
                                document.getElementById("stuInfoIframe").contentWindow.location.reload(true);
                            },
                            complete:function(){
                                //请求完成的处理
                                //alert("请求完成！");
                            },
                            error:function(){
                                //请求出错处理
                                alert("重复选择！");
                            }
                        });
                    } else {
                        alert("此课程人数已满！");
                    }
                });
            });
    </script>
</head>

<body >
<header style="text-align: right"><a  href="./ProjectManagement/TeacherLogin.jsp">退出登录</a></header>
<header style="text-align: left">
<%--    <input type="button"  onclick="window.location.href='http://localhost:8080/cjw_war_exploded/Olsystem/selectcheck.jsp';" value="选课结果查询" style="width:180px;height:40px;background-color:whitesmoke;font-family: 华文行楷,serif;font-size: 20px">--%>
    <input type="button"  onclick="window.location.href='GradeGetServlet';" value="学生成绩查询" style="width:180px;height:40px;background-color:whitesmoke;font-family: 华文行楷,serif;font-size: 20px">
    </header>
<h3 style="font-family:华文行楷,serif;text-align: center;font-size: 40px;color: cornflowerblue">学生选课界面</h3>
<%--<form action="SearchServlet" method="post">--%>
<%--    查找：<input type="text" name="search" value=<%=request.getParameter("search") == null?"":request.getParameter("search")%> >--%>
<%--    <input type="submit" value="搜索" name="ok" />--%>
<%--</form>--%>
<br><br>
<table class="table">
    <tr>
        <td>课程名称</td>
        <td>剩余量</td>
        <td>总数量</td>
        <td>任课教师</td>
        <td>课程地点</td>
        <td>课程时间</td>
        <td>课程时长</td>
        <td>操作</td>
    </tr>

    <c:forEach items="${requestScope.courseList}" var="courseList">
        <tr>
            <td>${courseList.courseName }</td>
            <td>${courseList.courseRemain }</td>
            <td>${courseList.courseTotal }</td>
            <td>${courseList.courseTeacher}</td>
            <td>${courseList.coursePlace }</td>
            <td>${courseList.courseTime }</td>
            <td>${courseList.courseTimelength }</td>
            <td>
                <input type="button" value="选中" name="select" id="${courseList.courseId}+${courseList.courseTeacherId}"/>
            </td>
        </tr>
    </c:forEach>
</table>

<br><br>
<hr>
<br><br>
<h2>我的选课</h2>
<frameset>
        <iframe style="width: 100%;" id="stuInfoIframe" src="stu" ></iframe>
</frameset>
</body>
</html>
